<template>
	<view>
		<!-- 导航栏 -->
		<!-- <view class="border-bottom border-light-secondary" style="margin-top: 100rpx;">
			<text class="iconfont icon-fanhui" style="font-size: 50rpx;padding-left: 20rpx;" @click="fanhui"></text>
			<text style="padding-left: 260rpx; padding-right: 220rpx;">消息列表</text>
			<text class="iconfont icon-geren"style="font-size: 50rpx;" @click="geren"></text>
		</view> -->
		<!-- 聊天列表 -->
		<scroll-view scroll-y="true" style="position: absolute;left: 0;top: 0;right: 0;bottom: 100rpx;" :scroll-into-view="scrollInto" scroll-with-animation>
			<block v-for="(item,index) in list " :key="index">
				<view :id="'chat'+index">
					<user-chat-list :item="item" :index="index" :pretime="index>0?list[index-1].create_time:0"></user-chat-list>
				</view>
			</block>
		</scroll-view>
		<!-- 底部操作条 -->
		<bottom-input @submit="submit"></bottom-input>
		<!-- <view style="height: 100rpx;" class="fixed-bottom flex align-cente border-top bg-white">
			<input type="text" v-model="content" value="" class="border flex-1 rounded bg-light justify-center align-center mr-1" style="padding: 5rpx; margin-top: 20rpx;margin-left: 20rpx;" placeholder="文明发言" @confirm="submit"/>
			<view @click="submit" class="iconfont icon-fabu flex align-center justify-center animate__animated "hover-class="animate__jello"  style="width: 100rpx; size: 100rpx;"></view>
		</view> -->
	</view>
</template>

<script>
	
	import userChatList  from '@/components/user-chat/user-chat-list.vue'
	import bottomInput  from '@/components/common/bottom-input.vue'
	export default {
		components:{
			userChatList,
			bottomInput
		},
		data() {
			return {
				id1:'',
				demo:{},
				scrollInto:"",
				list:[]
			}
		},
		//页面加载完成
		onReady(){
			this.pageToBottom()
		},
		onShow() {
			this.getdata()
			this.pageToBottom()
		},
		onLoad(e) {
			console.log(e.id)
			this.id1=e.id
			console.log(this.list)
			this.pageToBottom()
		},
		methods: {
			//得到数据
			async getdata(){
				
				const res = await uniCloud.callFunction({
					name: "userinformation",
					data: {
						action: 'getOne',
						id:this.$store.state.loginsStatus
					}
				})
				this.demo=res.result.data[0]
				console.log(this.demo)
				
				const eq = await uniCloud.callFunction({
					name: "msg",
					data: {
						action: 'getOne',
						id:this.$store.state.loginsStatus,
						id1:this.id1
					}
				})
				console.log(eq.result.data[0].list)
				console.log(this.list)
				 this.list=eq.result.data[0].list
				 
			},
			
			
			fanhui(){
			uni.navigateBack()
			},
			//发送
			async submit(data){
				
				let obj ={
					user_id:this.demo.id,
					avatar:this.demo.userpic,
					username:this.demo.username,
					data:data,
					type:"text",
					create_time:(new Date().getTime())
				}
				this.list.push(obj)
				// 加入数据库
				const res = await uniCloud.callFunction({
									name: 'msg',
									data: {
										action: 'update',
										id:this.$store.state.loginsStatus,
										id1:this.id1,
										list:this.list
									}
								})
				console.log(11111111)
				console.log(this.list)
				console.log(111111111)
				const w = await uniCloud.callFunction({
									name: 'msg',
									data: {
										action: 'update',
										id:this.id1,
										id1:this.$store.state.loginsStatus,
										list:this.list
									}
								})
				//清空
				this.content=''
				//滚动到底部
				this.pageToBottom()
			},
			//滚动到底部
			pageToBottom(){
				let lastIndex=this.list.length-1
				if(lastIndex<0)return;
				this.scrollInto='chat'+lastIndex
			}
		}
	}
</script>

<style>

</style>
